<?php

namespace app\api\controller\admin\setting;

use app\api\common\controller\BaseApi;
use app\api\services\admin\setting\ConfigServices;
use app\Request;
use think\App;
use think\Exception;

class ConfigController extends BaseApi
{

    private ConfigServices $services;

    /**
     * 构造函数
     *
     * @param \app\Request                                   $request
     * @param \think\App                                     $app
     * @param \app\api\services\admin\setting\ConfigServices $services
     */
    public function __construct(Request $request, App $app, ConfigServices $services)
    {
        parent::__construct($app);
        $this->services = $services;
    }

    /**
     * @param \app\Request $request
     * @param string       $name
     *
     * @return \think\response\Json
     */
    public function get(Request $request, string $name): \think\response\Json
    {
        $where = $request->getParams(['name']);
        $ret   = $this->services->getByDefault($where);
        return json($ret ?? []);
    }

    /**
     * 配置更新
     *
     * @param \app\Request $request
     *
     * @return \think\response\Json
     * @throws \think\Exception
     */
    public function update(Request $request): \think\response\Json
    {
        $data = $request->getParams([
            ['logo'],
            ['menu'],
            ['tab'],
            ['theme'],
            ['other'],
            ['header'],
        ]);
        if (empty($data)) {
            throw new Exception('非法参数请核对后重试');
        }
        $ret = $this->services->systemConfig($data);
        if ($ret)
            return ret_value(0, "Success");
        else
            return ret_value(-1, "Fail");
    }
}
